var form = document.querySelector('.center>form')
var username = document.querySelector('[name="username"]')
var nickname = document.querySelector('[name="nickname"]')
var password = document.querySelector('[name="password"]')
var rpassword = document.querySelector('[name="rpassword"]')
form.onsubmit = function () {
    var use = /^[a-z0-9]\w{4,11}$/
    if (!use.test(username.value)) {
        alert("用户名不符合规范，应该是字母数字开头，5~12位")
        return false
    }
    var pas = /\w{6,12}/
    if (!pas.test(password.value)) {
        alert("密码不符合规范，应该是字母数字下划线6~12位")
        return false
    }


    var xhr = new XMLHttpRequest;
    xhr.open('post', 'http://localhost:8888/users/register')
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
    xhr.send(`username=${username.value}&password=${password.value}&rpassword=${rpassword.value}&nickname=${nickname.value}`)
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
                var res = xhr.responseText;
                res = JSON.parse(res)
                console.log(res);
                var { code, message } = res
                alert(message)
                if (code === 1) {
                    location.href = 'login.html'
                }


            }
        }
    }
    return false
}

